﻿using Wicresoft.RDW.Interface;
using Wicresoft.RDW.Model;
using System.Collections.Generic;
using System.Text;
using System;
using Wicresoft.RDW.Utility;
using System.Configuration;

namespace Wicresoft.RDW.Presentation
{
    public class RequestPresent : BasePresent<Request, IRequest>
    {
        public static void UpdateRequestById(int requestId, int status, string comments)
        {
            present.UpdateRequestById(requestId, status, comments);
        }

        public static List<string> GetLabManagerAndSiteManagerEmail(int labId, int locationId)
        {
            var labManager = LabManagerPresent.GetByCondition(x => x.LabId == labId);
            if (labManager == null)
            {
                return null;
            }
            List<string> LabManagerAndSiteManagerEmails = new List<string>();
            if (labManager.RoleId.HasValue)
            {
                var labManagerEmails = GetEmailByManagerType(labManager.RoleId.Value);
                if (labManagerEmails != null && labManagerEmails.Count != 0)
                {
                    LabManagerAndSiteManagerEmails.AddRange(labManagerEmails);
                }
            }
            var location = LocationPresent.GetById(locationId);
            if (location == null)
            {
                return null;
            }
            var sietManager = SiteManagerPresent.GetByCondition(x => x.LocationId == location.LocationId);
            if (sietManager == null)
            {
                return null;
            }
            if (sietManager.RoleId.HasValue)
            {
                var sietManagerEmails = GetEmailByManagerType(sietManager.RoleId.Value);
                if (sietManagerEmails != null && sietManagerEmails.Count != 0)
                {
                    LabManagerAndSiteManagerEmails.AddRange(sietManagerEmails);
                }
            }

            return LabManagerAndSiteManagerEmails;
        }

        public static List<string> GetEmailByManagerType(int managerRoleId)
        {
            var UserRoles = UserRolePresent.GetList(x => x.RoleId == managerRoleId);
            if (UserRoles == null || UserRoles.Count <= 0)
            {
                return null;
            }
            List<string> Emails = new List<string>();
            foreach (var user in UserRoles)
            {
                var manager = UserPresent.GetById(user.UserId);
                if (manager == null)
                {
                    continue;
                }
                Emails.Add(manager.Email);
            }
            return Emails;
        }

        public static List<string> GetCenterManagerEmails(int managerRoleId)
        {
            return GetEmailByManagerType(managerRoleId);
        }

        //根据LabId获得实验室名字
        public static string GetLabNameById(int labid)
        {
            string labname = string.Empty;
            var lab = LabPresent.GetById(labid);
            if (lab != null)
            {
                labname = lab.Name;
            }
            return labname;
        }
        //根据ProductId获得产品中,英名称
        public static bool GetProductNameById(int productid, out string productNameCN, out string productNameEN)
        {
            productNameCN = string.Empty;
            productNameEN = string.Empty;
            var product = ProductPresent.GetById(productid);
            if (product != null)
            {
                productNameCN = product.Name_CN;
                productNameEN = product.Name_EN;
                return true;
            }
            return false;
        }

        //根据请求id获取链接地址
        public static string GetLinkAddressByRequestId(int requestId, string host, bool IsReadOnly, int port, Language language, bool IsOnlyRead)
        {
            if (IsReadOnly)
            {
                return "http://" + host + ":" + port + "/Request/Apply.aspx?pageStatus=Readonly&id=" + requestId + "&language=" + language.ToString() + "&read=" + IsOnlyRead;
            }
            return "http://" + host + ":" + port + "/Request/Apply.aspx?id=" + requestId + "&language=" + language.ToString() + "&read=" + IsOnlyRead;
        }

        //根据UserId获得收件人中英，文名字
        public static bool GetUserNameById(int userid, out string userNameCN, out string userNameEN)
        {
            userNameCN = string.Empty;
            userNameEN = string.Empty;
            var user = UserPresent.GetById(userid);
            if (user != null)
            {
                userNameCN = user.NameCN;
                userNameEN = user.NameEN;
                return true;
            }
            return false;
        }
        //根据UserId(即CurrentUserId)获得发件人邮箱和办公电话
        public static bool GetEmailPhoneById(int userid, out string email, out string officePhone)
        {
            email = "Qiming.Sang@alcatel-sbell.com.cn";
            officePhone = "2712-5684";
            return true;
        }

        public static string GetRequestUserEmail(int userId)
        {
            var user = UserPresent.GetById(userId);
            if (user != null)
            {
                return user.Email;
            }
            return string.Empty;
        }
    }
}
